package rack.lite.model

import rack.lite.utils.DbManager as db
import rack.lite.view.Console as con
import rack.lite.model.Aluno
import rack.lite.model.AlunoDao
import rack.lite.model.Disciplina
import rack.lite.model.DisciplinaDao

/**
 *
 * @author atila
 */
@Singleton class AlunoDisciplinaDao {

   private static String table = "aluno_disciplina.table"
   private static String tmpTable = "aluno_disciplina.table.tmp"

   def create(AlunoDisciplina ad) {
      def f = db.getTable(table)
      f.append(ad.toLine())
      println "${con.info} Relação Aluno_Disicplina criada com sucesso.\n"
   }

   def list() {
      def list = findAll()
      list.each { ad ->
         println ad
      }
   }

   def findAll() {
      def list = []
      def f = db.getTable(table)
      def lines = f.readLines()
      // lista de alunos
      def a_aluno = AlunoDao.instance.findAll()
      // lista de disciplinas
      def a_disc = DisciplinaDao.instance.findAll()
      lines.each {
         def a = it.split(":")
         def aluno = a_aluno.find { it.equals( new Aluno( login:a.getAt(0) ) ) }
         def disc = a_disc.find { it.equals( new Disciplina( codigo:a.getAt(1) ) ) }
         def ad = new AlunoDisciplina(aluno:aluno, disciplina:disc)
         list.push( ad )
      }

      return list
   }
}
